<?php
/*
Plugin Name: Mk-Small-Slider
Plugin URI: http://www.michaeljohndok.com/
Description: Description Small slider widget plugin
Version: 0.1
Author: Michael Johndok
Author URI: http://www.michaeljohndok.com/
*/

/**
* Copyright (c) `date "+%Y"` Your Name. All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
?>

<?php
/**
* new WordPress Widget format
* Wordpress 2.8 and above
* @see http://codex.wordpress.org/Widgets_API#Developing_Widgets
*/
class mk_slider_Widget extends WP_Widget {

    /**
     * Constructor
     *
     * @return void
     **/
	function mk_slider_Widget() {
	$widget_ops = array( 'classname' => 'mk_slider_css', 'description' => 'Mk-Small-Slider' );
	$this->WP_Widget( 'mk_slider_css', 'Mk-Small-Slider', $widget_ops );
	}

    /**
     * Outputs the HTML for this widget.
     *
     * @param array  An array of standard parameters for widgets in this theme
     * @param array  An array of settings for this widget instance
     * @return void Echoes it's output
     **/
	function widget( $args, $instance ) {


	//get var
	extract( $args );
	extract( $instance );
	$title = !empty( $instance['title'] ) ? $instance[ 'title' ] : '';
	$displaynum = !empty( $instance['displaynum'] ) ? $instance[ 'displaynum' ] : 8;
	$categoryid = !empty( $instance['categoryid'] ) ? $instance[ 'categoryid' ] : 38;

	//get books 
	 $args = array(
	'numberposts'     => $displaynum,
	'category'        => $categoryid,
	'orderby' 		  => 'rand',
	'order'           => 'DESC',
	'post_type'       => 'post',
	'post_status'     => 'publish',
	'suppress_filters' => true );

	 $myposts = get_posts( $args );




	echo $before_widget;
	echo $before_title;
	echo $title; // Can set this with a widget option, or omit altogether
	echo $after_title;

	?>
	<div id="slideshow">
		<?php foreach( $myposts as $post ) :	setup_postdata($post); ?>
			<?php 

			if ( has_post_thumbnail($post->ID)) {
	      // echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">';
	      echo get_the_post_thumbnail($post->ID, array(200,300));
	      // echo '</a>';
	    	}
		 ?>
		<?php endforeach ; ?>
	</div>


	<style type="text/css">

	/*****slidershow ****/
	#slideshow {
	  position:relative;
	  height:343px;
	      
	  }

	  #slideshow IMG {
	  position:absolute;
	  top:0;
	  left:0;
	  z-index:8;


	  }

	  #slideshow IMG.active {
	  z-index:10;
	  }

	  #slideshow IMG.last-active {
	  z-index:9;
	  }
	/*****end slidershow ****/
	</style>
	<script type="text/javascript">
	function slideSwitch() {
	  var $active = jQuery('#slideshow IMG.active');

	  if ( $active.length == 0 ) $active = jQuery('#slideshow IMG:last');

	  var $next =  $active.next().length ? $active.next()
	    : jQuery('#slideshow IMG:first');


	  $active.addClass('last-active');

	    
	  $next.css({opacity: 0.0})
	    .addClass('active')
	    .animate({opacity: 1.0}, 1000, function() {
	        $active.removeClass('active last-active');
	    });
	}

	jQuery(function() {
	setInterval( "slideSwitch()", 4000 );
	});
	</script>

	<?php

	echo $after_widget;
	}

    /**
     * Deals with the settings when they are saved by the admin. Here is
     * where any validation should be dealt with.
     *
     * @param array  An array of new settings as submitted by the admin
     * @param array  An array of the previous settings
     * @return array The validated and (if necessary) amended settings
     **/
	function update( $new_instance, $old_instance ) {
	// update logic goes here
	$updated_instance = $new_instance;
	return $updated_instance;
	}

    /**
     * Displays the form for this widget on the Widgets page of the WP Admin area.
     *
     * @param array  An array of the current settings for this widget
     * @return void Echoes it's output
     **/
	function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, array( 'title' => '',  'categoryid' => '', 'displaynum' => '' ) );
		$title = sanitize_title( $instance[ 'title' ] );
		$categoryid = esc_attr( $instance[ 'categoryid' ] );
		$displaynum = esc_attr( $instance[ 'displaynum' ] );
		?>
	    <p>
	        <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title (optional):','mkslider'); ?></label>
	        <input type="text" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" />
	    </p>
	    <p>
	        <label for="<?php echo $this->get_field_id('categoryid'); ?>"><?php _e('categoryid:','mkslider'); ?></label>
	        <input type="text" name="<?php echo $this->get_field_name('categoryid'); ?>" value="<?php echo esc_attr( $categoryid ); ?>" class="widefat" id="<?php echo $this->get_field_id('categoryid'); ?>" />
	    </p>
	    <p>
	        <label for="<?php echo $this->get_field_id('displaynum'); ?>"><?php _e('displaynum:','mkslider'); ?></label>
	        <input type="text" name="<?php echo $this->get_field_name('displaynum'); ?>" value="<?php echo $displaynum; ?>" class="widefat" id="<?php echo $this->get_field_id('displaynum'); ?>" />
	    </p>
	    <?php
	}
}

add_action( 'widgets_init', create_function( '', "register_widget('mk_slider_Widget');" ) );

?>